package cn.alcatraz.basic.controller;

import cn.alcatraz.basic.dto.SmsCodeDto;
import cn.alcatraz.basic.exception.BusinessException;
import cn.alcatraz.basic.service.IVerifyCodeService;
import cn.alcatraz.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 验证码图形
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/4 18:49
    * @Parameters: [imageCodeKey]
    * @Return cn.alcatraz.basic.utils.AjaxResult
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imageBase64Str = verifyCodeService.getImageCode(imageCodeKey);
            return AjaxResult.myself().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: smsCode
    * @Description: 手机验证码
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/6 18:53
    * @Parameters: [smsCodeDto]
    * @Return cn.alcatraz.basic.utils.AjaxResult
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return AjaxResult.myself();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    //emailCode
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            verifyCodeService.emailCode(smsCodeDto);
            return AjaxResult.myself();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
